package com.wye.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wye.dao.BrandMapper;
import com.wye.dao.CategoryMapper;
import com.wye.dao.SkuMapper;
import com.wye.dao.SpuMapper;
import com.wye.goods.pojo.*;
import com.wye.service.SpuService;
import entity.IdWorker;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SpuServiceImpl implements SpuService {

    private static Logger logger = LoggerFactory.getLogger(SpuServiceImpl.class);

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;


    @Override
    public void putMany(Long[] ids) {
        spuMapper.bacthUpdate(ids);
    }

    @Override
    public void put(Long spuId) {

        Spu spu = spuMapper.selectByPrimaryKey(spuId);

        if (spu.getIsDelete().equals("1")) {
            throw new RuntimeException("商品已经删除");
        }

        spu.setIsMarketable("1");
        spuMapper.updateByPrimaryKeySelective(spu);

    }

    @Override
    public void pull(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);

        if (spu.getIsDelete().equals("1")){
            throw new RuntimeException("商品已经删除");
        }

        spu.setIsMarketable("0");
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public void audit(Long id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);

        //判断该商品是否被删除
        if (spu.getIsDelete().equals("1")){
            throw new RuntimeException("该商品已被删除");
        }
        //审核
        spu.setIsMarketable("1");
        spu.setStatus("1");
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public Goods findGoodsById(Long id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);

        List<Sku> skuList = skuMapper.selectSkuList(id);

        Goods goods = new Goods();
        goods.setSkuList(skuList);
        goods.setSpu(spu);

        return goods;
    }

    @Override
    public void saveGoods(Goods goods) {
        Spu spu = goods.getSpu();

        if (spu.getId() == null){
            spu.setId(idWorker.nextId());
            spuMapper.insertSelective(spu);
        }else {
            skuMapper.deleteByPrimaryKey(spu.getId());
        }

        //查询3级分类
        Category category = categoryMapper.selectByPrimaryKey(spu.getCategory3Id());
        //查询品牌信息
        Brand brand = brandMapper.selectByPrimaryKey(spu.getBrandId());

        //sku
        Date currentTime = new Date();
        List<Sku> skuList = goods.getSkuList();
        for (Sku sku : skuList) {
            sku.setCreateTime(currentTime);
            sku.setId(idWorker.nextId());

            //拼接skuName

            if (StringUtils.isEmpty(sku.getSpec())){
                sku.setSpec("{}");
            }
            String name = spu.getName();

            Map<String, String> specMap = JSON.parseObject(sku.getSpec(), Map.class);
            for (Map.Entry<String, String> entry : specMap.entrySet()){
                name += " " + entry.getValue();
            }
            sku.setName(name);
            sku.setUpdateTime(currentTime);
            sku.setSpuId(spu.getId());
            sku.setCategoryId(spu.getCategory3Id());
            sku.setCategoryName(category.getName());
            sku.setBrandName(brand.getName());

            //将sku信息插入数据库
            skuMapper.insertSelective(sku);
        }
            
        
    }

    @Override
    public List<Spu> findAll() {
        return spuMapper.selectAll();
    }

    @Override
    public Spu findById(Integer id) {
        if (id != null){
            return spuMapper.selectByPrimaryKey(id.longValue());
        }
        return null;
    }

    @Override
    public int insert(Spu spu) {
        return spuMapper.insertSelective(spu);
    }

    @Override
    public void update(Spu spu) {
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public void delete(Integer id) {
        spuMapper.deleteByPrimaryKey(id.longValue());
    }

    @Override
    public List<Spu> findList(Spu spu) {
        return spuMapper.selectAll();
    }


}
